package com.tiger.cloud.controller.crm;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.Commucation;
import com.tiger.cloud.service.crm.ICommucationService;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 交流记录
 */
@Controller
@RequestMapping(value = "commucation")
@Api(value = "交流记录",tags = "交流记录")
public class CommucationController extends BaseController {

	private static final Logger logger=Logger.getLogger(CommucationController.class);
	
	@Autowired
	private ICommucationService commucationService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分页查找用户交流记录",notes = "")
	public Response find(@CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		Page<Commucation> datas=commucationService.page(commucation);
		return ResponseUtil.ok(datas);
	}
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查找全部用户交流记录",notes = "")
	public Response list(@CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		return ResponseUtil.ok(commucationService.find(commucation));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新用户交流记录",notes = "更新用户交流记录")
	public Response update(@CurrentUserId Long userId,@RequestBody Commucation commucation){
		if(commucation.getId()!=null&&commucation.getId()>0){
			Commucation updateEntity=commucationService.get(commucation.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			commucation.setUserId(userId);
			if(commucationService.update(commucation)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加用户交流记录",notes = "添加用户交流记录")
	public Response add(@CurrentUserId Long userId,@RequestBody Commucation commucation){
		commucation.setUserId(userId);
		commucation.setIsDelete(DeleteEnum.NOMAIL);
		commucationService.insert(commucation);
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除用户交流记录",notes = "标记删除用户交流记录")
	public Response delete(@CurrentUserId Long userId,@RequestBody Commucation commucation){
		if(commucation.getId()!=null&&commucation.getId()>0){
			Commucation deleteEntity=commucationService.get(commucation.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			commucation.setUserId(userId);
			commucation.setIsDelete(DeleteEnum.DELETE);
			if(commucationService.update(commucation)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}